﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

namespace CivilRegistry.Common
{
    public static class IEnumerableExtesions
    {
        public static List<T> OrderBy<T>(this IEnumerable<T> collection, string columnName, SortDirection direction)
        {
            ParameterExpression param = Expression.Parameter(typeof(T), "x"); // x
            Expression property = Expression.Property(param, columnName); //x.ColumnName
            Func<T, object> func = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, columnName), typeof(object)), param).Compile(); // x => x.ColumnName
            return SortExpressionBuilder<T>.CreateExpression(direction)(collection, func).ToList();
        }
    }
}

//Reference: http://www.codeproject.com/Articles/246361/Handle-GridView-OnSorting-and-create-sorting-expre